﻿#region Copyright 2008 - Evolution Systems.
//
// Author: José Rogério Ferreira dos Santos(joserogerio.santos@gmail.com)
// Date: 18/04/2009
// Time: 20:45
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: ${FILE_NAME}.cs
#endregion
using System;
using System.IO;
using System.Xml.Serialization;

namespace csharp.rad.finan
{
	/// <summary>
	/// Description of Configuration.
	/// </summary>
	public class Configuration
	{
		private string _path = "FinanConfig.xml";
		private string _toPayGroupId = Constants.EXPENSE;
		private string _toPaySubGroupId= "1";
		private string _toPayClassId= "1";
		private string _toPaySubClassId= "1";
		private string _toPayBillId= "1";
		
		private string _toReceiveGroupId= Constants.REVENUE;
		private string _toReceiveSubGroupId= "1";
		private string _toReceiveClassId= "1";
		private string _toReceiveSubClassId= "1";
		private string _toReceiveBillId= "1";		
		
		#region properties
		public string path 
		{
			get { return _path; }
			set { _path = value; }
		}
		
		public string toPayGroupId {
			get { return _toPayGroupId; }
			set { _toPayGroupId = value; }
		}
		
		public string toPaySubGroupId {
			get { return _toPaySubGroupId; }
			set { _toPaySubGroupId = value; }
		}
		
		public string toPayClassId {
			get { return _toPayClassId; }
			set { _toPayClassId = value; }
		}
		
		public string toPaySubClassId {
			get { return _toPaySubClassId; }
			set { _toPaySubClassId = value; }
		}
		
		public string toPayBillId {
			get { return _toPayBillId; }
			set { _toPayBillId = value; }
		}
		
		public string toReceiveGroupId {
			get { return _toReceiveGroupId; }
			set { _toReceiveGroupId = value; }
		}
		
		public string toReceiveSubGroupId {
			get { return _toReceiveSubGroupId; }
			set { _toReceiveSubGroupId = value; }
		}
		
		public string toReceiveClassId {
			get { return _toReceiveClassId; }
			set { _toReceiveClassId = value; }
		}
		
		public string toReceiveSubClassId {
			get { return _toReceiveSubClassId; }
			set { _toReceiveSubClassId = value; }
		}
		
		public string toReceiveBillId {
			get { return _toReceiveBillId; }
			set { _toReceiveBillId = value; }
		}
		
		
		#endregion
		

		public bool get()
		{
			return deserialize();
		}
	
		public bool save()
		{
			return doSerialization();
		}
		
		public void clone(Configuration c)
		{
			this.toPayGroupId = c.toPayGroupId;
			this.toPaySubGroupId = c.toPaySubGroupId;
			this.toPayClassId = c.toPayClassId;
			this.toPaySubClassId = c.toPaySubClassId;
			this.toPayBillId = c.toPayBillId;
			
			this.toReceiveGroupId = c.toReceiveGroupId;
			this.toReceiveSubGroupId = c.toReceiveSubGroupId;
			this.toReceiveClassId = c.toReceiveClassId;
			this.toReceiveSubClassId = c.toReceiveSubClassId;
			this.toReceiveBillId = c.toReceiveBillId;			
		}
		
		public bool doSerialization()
		{
			try
			{
				XmlSerializer xs = new XmlSerializer(typeof(Configuration));
				
				StreamWriter sw = new StreamWriter(_path);
				
				xs.Serialize(sw, this);
				
				sw.Close();
				
				return true;
			}
			catch
			{
				//_msg_erro = e.Message;
				return false;
			}
		}
		
		public bool deserialize()
		{
			try
			{
				Configuration configuration = new Configuration();
				
				XmlSerializer xs = new XmlSerializer(typeof(Configuration));
				
				FileStream fs = new FileStream(_path, FileMode.Open);
		
				configuration = ((Configuration) xs.Deserialize(fs));
				
				this.clone(configuration);
			
				fs.Close();
				return true;
			}
			catch(Exception e)
			{
				csharp.common.LogErro.writeError(e.Message);
				return false;
			}
		}
	}
}

